#include "ImgMgr.h"



ImgMgr::ImgMgr()
{

}

void ImgMgr::setImage(const std::string chemin, const int dimensionTile, const sf::Color maskColor)
{
    m_chemin = chemin;
    m_dimensionTile = dimensionTile;
    m_image.loadFromFile(chemin.c_str());
    m_image.createMaskFromColor(maskColor);
    m_texture.loadFromImage(m_image);
    m_texture.setSmooth(false);
    m_sprite.setTexture(m_texture, true);
}


sf::Sprite ImgMgr::getSprite(const int point)
{
    unsigned int P1X = 0;
    unsigned int P2Y = 0;
    int e = 0;

    for(e = 1; e < point; e++)
    {
        P1X += m_dimensionTile;
        if(P1X >= m_texture.getSize().x)
        {
            P1X = 0;
            P2Y += m_dimensionTile;
        }
    }
    m_sprite.setTextureRect(sf::IntRect(P1X, P2Y, m_dimensionTile, m_dimensionTile));
    return m_sprite;
}

sf::Sprite ImgMgr::getSprite(const int firstPoint, const int secondPoint)
{
    int P1X = 0;
    int P2Y = 0;
    unsigned int largeur = 0;
    unsigned int longeur = 0;
    int e = 0;

    for(e = 1; e < firstPoint; e++)
    {
        P1X += m_dimensionTile;
        if(P1X >= m_texture.getSize().x)
        {
            P1X = 0;
            P2Y += m_dimensionTile;
        }
    }

    for(e = 1; e < secondPoint; e++)
    {
        largeur += m_dimensionTile;
        if(largeur >= m_texture.getSize().x)
        {
            largeur = 0;
            longeur += m_dimensionTile;
        }
    }
    m_sprite.setTextureRect(sf::IntRect(P1X, P2Y, largeur + m_dimensionTile - P1X, longeur + m_dimensionTile - P2Y));
    return m_sprite;
}

void ImgMgr::maj()
{
    m_image.loadFromFile(m_chemin.c_str());
    m_texture.update(m_image);
}

void ImgMgr::setScaleSprite(const int scale)
{
    m_sprite.setScale(scale, scale);
}

sf::Sprite &ImgMgr::getSprite()
{
    return m_sprite;
}

sf::Texture &ImgMgr::getTexture()
{
    return m_texture;
}

sf::Image &ImgMgr::getImage()
{
    return m_image;
}

ImgMgr::~ImgMgr()
{

}
